#!/bin/bash
set -uxe

PROJECT="$1"
MODULE="$2"

CUR_DIR=$(cd `dirname $0`;pwd)
CI_DIR=$(cd $CUR_DIR/../../../..;pwd)
LLT_DIR='/usr1/LLT'
TESTCODE="$CI_DIR/llt/llt/${MODULE}"
CUnit="$CI_DIR/llt/llt/CUnit"
BUILD_ROOT="/usr1/LLT/$PROJECT/$MODULE/build-root"

service_file_path="$LLT_DIR/$PROJECT/$MODULE/_service"
url=`cat $service_file_path | grep -v '<!--' | grep 'name="url"' | awk -F'<|>' '{print $3}'| sed 's/[ ]*//g'`
subdir=`cat $service_file_path | grep -v '<!--' | grep 'name="subdir"' | awk -F'<|>' '{print $3}' | sed 's/[ ]*//g'`
SRC_PATH="$CI_DIR/next_source/${url#*/}/$subdir"
SRC_NAME=`basename $SRC_PATH`

cd $CUR_DIR
rm -rf $BUILD_ROOT/LLT
mkdir -p $BUILD_ROOT/LLT/src
cp -r $CUR_DIR/{do_lcov.sh,ut_lcov.sh,ut_report} "$BUILD_ROOT/LLT"
cp -r "$TESTCODE" "$CUnit" "$BUILD_ROOT/LLT"
cp -r "$SRC_PATH" "$BUILD_ROOT/LLT/src"
cp -r $BUILD_ROOT/LLT/ut_report/* $BUILD_ROOT/LLT/$MODULE
if ! mount -l | grep "$BUILD_ROOT/proc"; then
    mount --bind /proc $BUILD_ROOT/proc
fi
chroot $BUILD_ROOT sh /LLT/${MODULE}/CUnit.sh /LLT/src/$SRC_NAME
chroot $BUILD_ROOT sh /LLT/do_lcov.sh ${MODULE}

#**************new
mv $build_root/html ${WORKSPACE}

#生成cunit测试的报告的html文件
mkdir -p ${WORKSPACE}/ut_report
cd ${BUILD_ROOT}/LLT/src/${MODULE}
for dir in `ls -F |grep "/$"`
do
    dir=${dir%/}
    if [ ! -f ${dir}/Automated-Results.xml ]; then
        continue
    fi
    cp -a ${dir} ${WORKSPACE}/ut_report/
    #cp -a ${WORKSPACE}/html ${WORKSPACE}/ut_report/${dir}/results
done


cp -a ${CUR_DIR}/gen_cunit_report.py ${WORKSPACE}/ut_report/
cp -a ${CUR_DIR}/ut_css/* ${WORKSPACE}/ut_report/
cd ${WORKSPACE}/ut_report/
if rpm -qa |grep "libxslt-python"
then
    echo "installed"
else
    zypper in -n libxslt-python-1.1.24-19.21.1
fi
python gen_cunit_report.py
if [ $? -ne 0 ]; then
    echo "ERROR: Some case failed in cunit,please check"
    exit 1
fi

